import 'package:flutter/material.dart';
import 'package:wan_android/api/home.dart';

import 'home_article_type.dart';
import 'home_banner_type.dart';

class HomePageModel extends ChangeNotifier {
  int page = 0;
  List<BannerItemData> _bannerList = [];
  List<BannerItemData> get bannerList => _bannerList;

  List<ArticleItemData> _articleList = [];
  List<ArticleItemData> get articleList => _articleList;


  Future getBanner() async {
    Map<String, dynamic> response = await getBannerList();
    HomeBannerResp homeBannerResp = HomeBannerResp.fromJson(response);
    if (homeBannerResp.data != null) {
      _bannerList = homeBannerResp.data;
    } else {
      _bannerList = [];
    }
    notifyListeners();
  }

  Future getArticle(bool loadMore) async {
    if (loadMore) {
      page++;
      Map<String, dynamic> response = await getArticleList(page);
      HomeArticleResp homeArticleResp = HomeArticleResp.fromJson(response);

      if (homeArticleResp.data != null && homeArticleResp.data.datas != null) {
        // _articleList = homeArticleResp.data.datas;
        _articleList.addAll(homeArticleResp.data.datas);
      } else {
        _articleList = [];  
      }
    } else {
      page = 0;
      Map<String, dynamic> response = await getArticleList(page);
      HomeArticleResp homeArticleResp = HomeArticleResp.fromJson(response);

      _articleList.clear();
      if (homeArticleResp.data != null && homeArticleResp.data.datas != null) {
        _articleList = homeArticleResp.data.datas;
      } else {
        _articleList = [];  
      }
    }
    notifyListeners();
  }
}